package com.lmx.videoplay.image;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.lmx.videoplay.R;
import com.lmx.videoplay.library.VideoPlayAdapter;

import java.util.ArrayList;
import java.util.List;

public class ImageDetailsAdapter extends VideoPlayAdapter<ImageDetailsAdapter.ViewHolder> {

    public List<String> imgList;
    private Context mContext;
    private int mCurrentPosition;
    private OnPageSelectedLister lister;

    public ImageDetailsAdapter(Context mContext, OnPageSelectedLister pageSelectedLister) {
        this.mContext = mContext;
        this.lister = pageSelectedLister;
        this.imgList = new ArrayList<>();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_image_details, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        RequestOptions options = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with(mContext)
                .load(imgList.get(position))
                .apply(options)
                .into(holder.img);
    }

    @Override
    public int getItemCount() {
        return this.imgList.size();
    }

    @Override
    public void onPageSelected(int itemPosition, View itemView) {
        mCurrentPosition = itemPosition;
        if(lister != null){
            lister.onPageSelected(itemPosition);
        }
    }

    public void setData(List<String> imgList) {
        if (this.imgList != null) {
            if (this.imgList.size() > 0) {
                this.imgList.clear();
            }
        }
        this.imgList.addAll(imgList);
        this.notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView img;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.img);
        }
    }

    public interface OnPageSelectedLister {
        void onPageSelected(int position);
    }
}